package com.link.linkupjavaweb.service.impl;

import com.link.linkupjavaweb.dto.GameResultDTO;
import com.link.linkupjavaweb.entity.GameRecord;
import com.link.linkupjavaweb.entity.User;
import com.link.linkupjavaweb.mapper.GameRecordMapper;
import com.link.linkupjavaweb.mapper.UserMapper;
import com.link.linkupjavaweb.service.GameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 游戏Service实现类
 */
@Service
public class GameServiceImpl implements GameService {
    
    @Autowired
    private GameRecordMapper gameRecordMapper;
    
    @Autowired
    private UserMapper userMapper;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public GameRecord saveGameRecord(Long userId, GameResultDTO resultDTO) {
        // 1. 创建游戏记录
        GameRecord record = new GameRecord();
        record.setUserId(userId);
        record.setGameType(1); // 单机模式
        record.setGameMode(resultDTO.getGameMode());
        record.setScore(resultDTO.getScore());
        record.setMaxCombo(resultDTO.getMaxCombo());
        record.setPairsCleared(resultDTO.getPairsCleared());
        record.setTotalPairs(resultDTO.getTotalPairs());
        record.setUseTime(resultDTO.getUseTime());
        record.setResult(resultDTO.getResult());
        record.setItemsUsed(resultDTO.getItemsUsed());
        record.setCreateTime(LocalDateTime.now());
        
        // 2. 保存游戏记录
        gameRecordMapper.insert(record);
        
        // 3. 更新用户统计数据
        User user = userMapper.selectById(userId);
        if (user != null) {
            // 累加总积分
            user.setTotalScore(user.getTotalScore() + resultDTO.getScore());
            
            // 更新游戏次数
            user.setGameCount(user.getGameCount() + 1);
            
            // 更新胜负次数
            if (resultDTO.getResult() == 2) { // 完成
                user.setWinCount(user.getWinCount() + 1);
            } else if (resultDTO.getResult() == 0 || resultDTO.getResult() == 3) { // 失败或超时
                user.setLoseCount(user.getLoseCount() + 1);
            }
            
            // 更新最大连击
            if (resultDTO.getMaxCombo() > user.getMaxCombo()) {
                user.setMaxCombo(resultDTO.getMaxCombo());
            }
            
            userMapper.updateById(user);
        }
        
        return record;
    }
    
    @Override
    public List<GameRecord> getUserGameRecords(Long userId, Integer limit) {
        return gameRecordMapper.selectUserGameRecords(userId, limit);
    }
    
    @Override
    public Object getUserGameStats(Long userId) {
        Map<String, Object> stats = new HashMap<>();
        
        // 总游戏次数
        Integer totalGames = gameRecordMapper.countUserGames(userId);
        stats.put("totalGames", totalGames);
        
        // 最高分
        Integer maxScore = gameRecordMapper.selectUserMaxScore(userId);
        stats.put("maxScore", maxScore != null ? maxScore : 0);
        
        // 用户信息
        User user = userMapper.selectById(userId);
        if (user != null) {
            stats.put("totalScore", user.getTotalScore());
            stats.put("winCount", user.getWinCount());
            stats.put("loseCount", user.getLoseCount());
            stats.put("maxCombo", user.getMaxCombo());
        }
        
        return stats;
    }
}

